<?php
class storeAction extends backendAction
{
    public function _initialize() {
        parent::_initialize();
        $this->_cate_mod = D('region');
        $this->_mod = D('store');
        //var_dump($tihs->_mod);
    }
    
    public function _before_index() {  

        $regionList = F('region_list');
        
        if(empty($regionList)){
            $regionList = D('region')->cate_cache();
        }
        $this->readyData();
        $this->assign('regionList',$regionList); 
      
    }  
    
    public function _before_add(){
         $this->readyData();
    }  
    
    /**
     * 给模板准备数据
     */ 
    private function readyData(){
    if (IS_POST){
        
        $_POST['gps'] = $_POST['positionX'].','.$_POST['positionY'];
    }else{
        //查数据
        $bcList = F('bc_list');
        if(empty($bcList)){
            $bcList = D('bc')->cate_cache();
        }
        $customerList = F('customer_list');
        if(empty($customerList)){
            $customerList = D('customer')->cate_cache();
        }        
        $this->assign('customerList',$customerList);
        $this->assign('bcList',$bcList);  
    }           
        
        
    }
    
    public function _before_edit(){
        $this->readyData(); 
        if(!IS_POST){
            // 查找分类 spid; 
            $id = $this->_get('id','intval');
            $spid = M('region')->where(array('id'=>$id))->getField('spid');       
            $this->assign('spid',$spid);
        }
        
    }
    
   protected function _search() {
        $map = array();
        ($channel = $this->_request('channel', 'trim')) && $map['channel'] = $channel;
        ($name = $this->_request('name', 'trim')) && $map['name'] = array('like', '%'.$name.'%');
        $cate_id = $this->_request('regionId', 'intval');
        $selected_ids = '';
        if ($cate_id) {
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $map['regionId'] = array('IN', $id_arr);
            $spid = $this->_cate_mod->where(array('id'=>$cate_id))->getField('spid');
            $selected_ids = $spid ? $spid . $cate_id : $cate_id;
        }
        $this->assign('search', array(
            'name'=>$name,
            'cate_id' => $cate_id,
            'selected_ids' => $selected_ids,
            'channel'  => $channel,
            
        ));
        return $map;
    }
    
    
    /**
     * 取得指定地区下面的厅店
     */
    public function getStoreList(){
        $cate_id = $this->_request('region_id','intval');
       // var_dump($cate_id);
        $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
       // var_dump($id_arr);
        $map['regionId'] = array('IN', $id_arr);        
        $list = $this->_mod->where($map)->getField('id,name');
        if(!empty($list)){
            $this->ajaxReturn(1,'',$list);
        }else{
            $this->ajaxReturn(0);
        } 
    }
    
    public function check_cmts_url(){
        $ctms = $this->_request('ctms');
        $id = $this->_get('id', 'intval',0);  
        if(!empty($ctms)){
            $id = M('store')->where('ctms="'.$ctms.'" and `id`!='.$id)->getField('id');
            if($id){
                echo 0;
            }else{
                echo 1;
            }
        }
    }
    
    
}

?>